package com.example.demo.controller.system;

import com.example.demo.annoation.Log;
import com.example.demo.bean.AjaxRes;
import com.example.demo.bean.sysconfig.SysConfigDO;
import com.example.demo.controller.BaseAdminController;
import com.example.demo.enums.BusinessType;
import com.example.demo.service.ISysConfigService;
import com.example.demo.dto.sysconfig.SysConfigPageReqVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping(value = "/system/sys_config")
@RestController
public class SysConfigController extends BaseAdminController {

    @Resource
    private ISysConfigService sysConfigService;

    @GetMapping("/list")
    public AjaxRes list(SysConfigPageReqVO sysConfigPageReqVO) {
        return AjaxRes.success(sysConfigService.list(sysConfigPageReqVO));
    }

    @GetMapping("/listByConfigKey")
    public AjaxRes listByConfigKey(@RequestParam("configKey") String[] configKey) {
        return AjaxRes.success(sysConfigService.listByConfigKey(configKey));
    }

    @PostMapping("/insert")
    @Log(title = "新增配置", businessType = BusinessType.INSERT)
    public AjaxRes insert(@RequestBody SysConfigDO sysConfigDO) {
        return toAjax(sysConfigService.addSysConfig(sysConfigDO));
    }

    @DeleteMapping("/delete/{sysConfigId}")
    @Log(title = "删除配置", businessType = BusinessType.DELETE)
    public AjaxRes delete(@PathVariable("sysConfigId") Long sysConfigId) {
        return toAjax(sysConfigService.deleteSysConfig(new SysConfigDO().setId(sysConfigId)));
    }

    @PutMapping("/update")
    @Log(title = "修改配置", businessType = BusinessType.UPDATE)
    public AjaxRes update(@RequestBody SysConfigDO sysConfigDO) {
        return toAjax(sysConfigService.updateSysConfig(sysConfigDO));
    }

    @GetMapping("/{sysConfigId}")
    public AjaxRes query(@PathVariable("sysConfigId") Long sysConfigId) {
        return AjaxRes.success(sysConfigService.getSysConfig(new SysConfigDO().setId(sysConfigId)));
    }
}
